﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;

namespace HotelMgr.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class RoomTypeController : Controller
    {
        private HotelEntities db = new HotelEntities();

        //
        // GET: /RoomType/

        public ViewResult Index()
        {
            return View(db.RoomTypes.ToList());
        }

        //
        // GET: /RoomType/Details/5

        public ViewResult Details(int id)
        {
            RoomType roomtype = db.RoomTypes.Find(id);
            return View(roomtype);
        }

        //
        // GET: /RoomType/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /RoomType/Create

        [HttpPost]
        public ActionResult Create(RoomType roomtype)
        {
            if (ModelState.IsValid)
            {
                db.RoomTypes.Add(roomtype);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(roomtype);
        }
        
        //
        // GET: /RoomType/Edit/5
 
        public ActionResult Edit(int id)
        {
            RoomType roomtype = db.RoomTypes.Find(id);
            return View(roomtype);
        }

        //
        // POST: /RoomType/Edit/5

        [HttpPost]
        public ActionResult Edit(RoomType roomtype)
        {
            if (ModelState.IsValid)
            {
                db.Entry(roomtype).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(roomtype);
        }

        //
        // GET: /RoomType/Delete/5
 
        public ActionResult Delete(int id)
        {
            RoomType roomtype = db.RoomTypes.Find(id);
            return View(roomtype);
        }

        //
        // POST: /RoomType/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            RoomType roomtype = db.RoomTypes.Find(id);
            db.RoomTypes.Remove(roomtype);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        #region RoomTypeGrid
        public ActionResult Select()
        {

            return View("Index", db.RoomTypes.ToList());
        }
        public ActionResult Insert()
        {
            RoomType roomtype = new RoomType();
            if (TryUpdateModel(roomtype))
            {
                db.RoomTypes.Add(roomtype);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("Index", db.RoomTypes.ToList());
        }
        public ActionResult Save(int id)
        {
            RoomType roomtype = new RoomType { RoomTypeID = id };
            if (TryUpdateModel(roomtype))
            {
                db.Entry(roomtype).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View("Index", db.RoomTypes.ToList());
        }
        public ActionResult Delete()
        {
            
            return View("Index", db.RoomTypes.ToList());
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}